package com.zzh.takeoutapp.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.zzh.takeoutapp.R;
import com.zzh.takeoutapp.model.PhotoInfo;
import com.zzh.takeoutapp.utils.DensityUtil;

import java.util.List;

/**
 * @author shoyu
 * @ClassName MultiImageView.java
 * @Description: 显示1~N张图片的View
 */

public class MultiImageView extends LinearLayout {
    public static int MAX_WIDTH = 0;

    // 照片的Url列表
    private List<PhotoInfo> imagesList;
    /**
     * 长度 单位为Pixel
     **/
    private int pxOneMaxWandH;  // 单张图最大允许宽高
    private int pxMoreWandH = 0;// 多张图的宽高
    private int pxImagePadding = DensityUtil.dip2px(getContext(), 3);// 图片间的间距

    private int MAX_PER_ROW_COUNT = 3;// 每行显示最大数
    private LayoutParams onePicPara;
    private RelativeLayout.LayoutParams morePara, moreParaColumnFirst;
    private LayoutParams rowPara;
    private Context mContext;
    private OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }

    private OnItemClickListenerDelete mOnItemClickListenerDelete;

    public void setOnItemClickListenerDelete(OnItemClickListenerDelete onItemClickListener) {
        mOnItemClickListenerDelete = onItemClickListener;
    }

    public List<PhotoInfo> getImagesList() {
        return imagesList;
    }

    public void setImagesList(List<PhotoInfo> imagesList) {
        this.imagesList = imagesList;
    }

    public MultiImageView(Context context) {
        super(context);
        mContext = context;
    }

    public MultiImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
    }

    public void removeListPhoto(int position) {
        if (imagesList != null && imagesList.size() > 0) {
            imagesList.remove(position);
        }
        requestFocus();
        if (MAX_WIDTH > 0) {
            pxMoreWandH = (MAX_WIDTH - pxImagePadding * 2) / 4; //解决右侧图片和内容对不齐问题
            pxOneMaxWandH = MAX_WIDTH * 2 / 4;
            initImageLayoutParams();
        }
        initView();
    }

    public List<PhotoInfo> getList() {
        return imagesList;
    }

    public void setList(List<PhotoInfo> lists) throws IllegalArgumentException {
        if (lists == null) {
            throw new IllegalArgumentException("imageList is null...");
        }
        imagesList = lists;
        if (MAX_WIDTH > 0) {
            pxMoreWandH = (MAX_WIDTH - pxImagePadding * 2) / 4; //解决右侧图片和内容对不齐问题
            pxOneMaxWandH = MAX_WIDTH * 2 / 4;
            initImageLayoutParams();
        }
        initView();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if (MAX_WIDTH == 0) {
            int width = measureWidth(widthMeasureSpec);
            if (width > 0) {
                MAX_WIDTH = width;
                if (imagesList != null && imagesList.size() > 0) {
                    setList(imagesList);
                }
            }
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    /**
     * Determines the width of this view
     *
     * @param measureSpec A measureSpec packed into an int
     * @return The width of the view, honoring constraints from measureSpec
     */
    private int measureWidth(int measureSpec) {
        int result = 0;
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);

        if (specMode == MeasureSpec.EXACTLY) {
            result = specSize;
        } else {
            if (specMode == MeasureSpec.AT_MOST) {
                result = Math.min(result, specSize);
            }
        }
        return result;
    }

    private void initImageLayoutParams() {
        int wrap = LayoutParams.WRAP_CONTENT;
        int match = LayoutParams.WRAP_CONTENT;
        onePicPara = new LayoutParams(wrap, wrap);
        moreParaColumnFirst = new RelativeLayout.LayoutParams(pxMoreWandH, pxMoreWandH);
        morePara = new RelativeLayout.LayoutParams(pxMoreWandH, pxMoreWandH);
        morePara.setMargins(pxImagePadding, 0, 0, 0);
        rowPara = new LayoutParams(match, wrap);
    }

    // 根据imageView的数量初始化不同的View布局,还要为每一个View作点击效果
    private void initView() {
        this.setOrientation(VERTICAL);
        this.removeAllViews();
        if (MAX_WIDTH == 0) {
            //为了触发onMeasure()来测量MultiImageView的最大宽度，MultiImageView的宽设置为match_parent
            addView(new View(getContext()));
            return;
        }
        if (imagesList == null || imagesList.size() == 0) {
            return;
        }
        /*if (imagesList.size() == 1) {
            addView(createImageView(0, false));
        } else {*/
        int allCount = imagesList.size();
        if (allCount == 4) {
            MAX_PER_ROW_COUNT = 4;
        } else {
            MAX_PER_ROW_COUNT = 4;
        }
        int rowCount = allCount / MAX_PER_ROW_COUNT
                + (allCount % MAX_PER_ROW_COUNT > 0 ? 1 : 0);// 行数
        for (int rowCursor = 0; rowCursor < rowCount; rowCursor++) {
            LinearLayout rowLayout = new LinearLayout(getContext());
            rowLayout.setOrientation(LinearLayout.HORIZONTAL);
            rowLayout.setLayoutParams(rowPara);
            if (rowCursor != 0) {
                rowLayout.setPadding(0, pxImagePadding, 0, 0);
            }
            int columnCount = allCount % MAX_PER_ROW_COUNT == 0 ? MAX_PER_ROW_COUNT
                    : allCount % MAX_PER_ROW_COUNT;//每行的列数
            if (rowCursor != rowCount - 1) {
                columnCount = MAX_PER_ROW_COUNT;
            }
            addView(rowLayout);
            int rowOffset = rowCursor * MAX_PER_ROW_COUNT;// 行偏移
            for (int columnCursor = 0; columnCursor < columnCount; columnCursor++) {
                int position = columnCursor + rowOffset;
                rowLayout.addView(createImageView(position, true));
            }
        }
        //}
    }

    private RelativeLayout createImageView(final int position, final boolean isMultiImage) {
        PhotoInfo photoInfo = imagesList.get(position);
        RelativeLayout relativeLayout = (RelativeLayout) View.inflate(getContext(), R.layout.item_dele_imageview, null);
        ImageView imageView = relativeLayout.findViewById(R.id.iv_image);
        ImageView iv_images = relativeLayout.findViewById(R.id.iv_images);
        iv_images.setAdjustViewBounds(true);
        iv_images.setScaleType(ScaleType.FIT_XY);
        if (isMultiImage) {
            imageView.setScaleType(ScaleType.FIT_XY);
            imageView.setLayoutParams(position % MAX_PER_ROW_COUNT == 0 ? moreParaColumnFirst : morePara);
        } else {
            imageView.setAdjustViewBounds(true);
            imageView.setScaleType(ScaleType.FIT_XY);
            int expectW = photoInfo.w;
            int expectH = photoInfo.h;
            if (expectW == 0 || expectH == 0) {
                imageView.setLayoutParams(onePicPara);
            } else {
                int actualW = 0;
                int actualH = 0;
                float scale = ((float) expectH) / ((float) expectW);
                if (expectW > pxOneMaxWandH) {
                    actualW = pxOneMaxWandH;
                    actualH = (int) (actualW * scale);
                } else if (expectW < pxMoreWandH) {
                    actualW = pxMoreWandH;
                    actualH = (int) (actualW * scale);
                } else {
                    actualW = expectW;
                    actualH = expectH;
                }
                imageView.setLayoutParams(new RelativeLayout.LayoutParams(actualW, actualH));
            }
        }
        imageView.setId(photoInfo.url.hashCode());
        imageView.setOnClickListener(new ImageOnClickListener(position));
        imageView.setOnLongClickListener(new ImageOnClickListener(position));
        imageView.setBackgroundColor(getResources().getColor(R.color.white));
        Glide.with(getContext()).load(photoInfo.url).diskCacheStrategy(DiskCacheStrategy.ALL).crossFade().into(imageView);
        if (position == 0) {
        } else {
            iv_images.setVisibility(VISIBLE);
            iv_images.setOnClickListener(new ImageOnClickListenerDelete(position));
        }
        return relativeLayout;
    }

    private class ImageOnClickListener implements OnClickListener, OnLongClickListener {

        private int position;

        public ImageOnClickListener(int position) {
            this.position = position;
        }

        @Override
        public void onClick(View view) {
            if (mOnItemClickListener != null) {
                mOnItemClickListener.onItemClick(view, position);
            }
        }

        @Override
        public boolean onLongClick(View view) {
            if (mOnItemClickListener != null) {
                mOnItemClickListener.onItemLongClick(view, position);
            }
            return false;
        }
    }

    private class ImageOnClickListenerDelete implements OnClickListener {

        private int position;

        public ImageOnClickListenerDelete(int position) {
            this.position = position;
        }

        @Override
        public void onClick(View view) {
            if (mOnItemClickListenerDelete != null) {
                mOnItemClickListenerDelete.onItemClick(view, position);
            }
        }
    }

    public interface OnItemClickListenerDelete {
        void onItemClick(View view, int position);
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);

        void onItemLongClick(View view, int position);
    }
}